package users

import (
	"errors"
	"time"

	"gitee.com/xgotool/backend-framework-template/config"
	"gitee.com/xgotool/backend-framework-template/model"
)

// 创建菜单
//
//	m	待创建的菜单
func MenuCreate(mod *model.Menu) error {
	conn, err := config.GetConnect()
	if err != nil {
		return err
	}
	mod.CreateTime = time.Now().Unix()
	mod.UpdateTime = mod.CreateTime
	if mod.Id > 0 {
		old := model.Menu{}
		err = conn.Table(old.TableName()).Select("id", "create_time").Where("id", mod.Id).First(&old).Error
		if err != nil {
			return err
		}
		if old.Id == 0 {
			return errors.New("菜单不存在")
		}
		mod.CreateTime = old.CreateTime
	}
	err = conn.Table(mod.TableName()).Save(mod).Error
	return err
}
